:-module(gua,[]).
:-use_module('de.pro').


%错综
cuo(Gua1,Gua2):-
	Count is (1 << Gua1.level) - 1,
	V is Count - Gua1.value,
	Gua2 = Gua1.put(value,V).

zong(Gua1,Gua2):-
	num2bin(Gua1.value,Gua1.level,B1),
	reverse(B1,B2),
	bin2num(B2,V),
	Gua2 = Gua1.put(value,V).

yang(Gua,Num):-
	num2bin(Gua.value,Gua.level,B),
	sumlist(B,Num).

% 1卦，2类型，3第几个（0Base）, 4求出的爻位(0Base)
yaowei(Gua,1,Order,Result):-
	num2bin(Gua.value,Gua.level,B),
	append(HL,_,B),
	O is Order + 1,
	sumlist(HL,O),!,
	length(HL,R),
	Result is R - 1.
yaowei(Gua,0,Order,Result):-
	cuo(Gua,G),
	yaowei(G,1,Order,Result).

change(Gua,Yao,Result):-
	integer(Yao),
	A is (1 << (Gua.level - 1 - Yao)) xor Gua.value,
	Result = Gua.put(value,A),!.
change(Gua,[],Gua):-!.
change(Gua,[H|T],Result):-
	change(Gua,H,G),
	change(G,T,Result).


